var popupMenuFlag = false;
function bindLongPressListener(role,managerlist) {
    // bubble menu
  
    if("MANAGER"==role){
        interaction_longPress_ForManager();
    }else if("ADMIN"==role){
        interaction_longPress_ForAdmin(managerlist);
    }else{
        interaction_longPress_ForAudience();
    }
    document.getElementById('scroll-2').addEventListener("touchstart", function(event) {
                if ($("#popMenu").length > 0) {
                    $("#popMenu").remove();
                    popupMenuFlag = false;
                }
            }, false);
}

function setPostion(ul,event){
     var clientX = event.touches[0].clientX;
     var toLeft = 0 ;
     if( (clientX+$(".dropdown-menu").width()) > $("body").width() ){
        toLeft = clientX - $(".dropdown-menu").width();
     }
     else{
        toLeft = event.touches[0].clientX +10;
     }

     ul.css({
        left: toLeft + 'px',
        top: (event.touches[0].clientY -50) + 'px'
     });
}

function interaction_longPress_ForAudience(){

    var timeout = undefined;
    $("#mutual dl.msg-left").each(function() {
        var _this = this;
        if(undefined==$(_this).attr("bindLongPress")){
            _this.addEventListener("touchstart", function(event) {
                // event.stopPropagation();
                timeout = setTimeout(function() {
                    var ele = $(event.target).closest(".live-msg-bubble").get(0);
                    if (ele && (ele.nodeName == "DL")) {
                        if (!popupMenuFlag) {
                            var ul = $("<ul/>", {
                                class: "dropdown-menu",
                                id: "popMenu"
                            });

                            var lis = "<li onclick=\"goToReplyState(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).attr("msgid")+"\')\"><a href='#'>回复</a></li>";
                            
                            ul.append(lis);
                            $("body").append(ul);
                            setPostion(ul,event);
                            popupMenuFlag = true;
                        }
                    }
                }, 1000);
            }, false);
            _this.addEventListener("touchend", function() {
                clearTimeout(timeout);
            }, false);
            _this.addEventListener("touchstart", function(event) {
                if ($("#popMenu").length > 0) {
                    $("#popMenu").remove();
                    popupMenuFlag = false;
                }
            }, false);
            $(_this).attr("bindLongPress","yes")
        }
        
    });
}


function interaction_longPress_ForManager(){

    var timeout = undefined;
    $("#mutual dl.msg-left").each(function() {
        var _this = this;
        if(undefined==$(_this).attr("bindLongPress")){
            _this.addEventListener("touchstart", function(event) {
                // event.stopPropagation();
                timeout = setTimeout(function() {
                    var ele = $(event.target).closest(".live-msg-bubble").get(0);
                    if (ele && (ele.nodeName == "DL")) {
                        if (!popupMenuFlag) {
                            var ul = $("<ul/>", {
                                class: "dropdown-menu",
                                id: "popMenu"
                            });

                            var lis = "<li onclick=\"moveToLiveArea(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).find(".msg-content > p").text()+"\')\"><a href='#'>移到直播区</a></li>"+
                                    "<li onclick=\"goToReplyState(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).attr("msgid")+"\')\"><a href='#'>回复</a></li>";
                        
                            ul.append(lis);
                            $("body").append(ul);
                            setPostion(ul,event);
                            popupMenuFlag = true;
                        }
                    }
                }, 1000);
            }, false);
            _this.addEventListener("touchend", function() {
                clearTimeout(timeout);
            }, false);
            _this.addEventListener("touchstart", function(event) {
                if ($("#popMenu").length > 0) {
                    $("#popMenu").remove();
                    popupMenuFlag = false;
                }
            }, false);
            $(_this).attr("bindLongPress","yes")
        }
        
    });

     $("#mutual dl.msg-right").each(function() {
        var _this = this;
        if(undefined==$(_this).attr("bindLongPress")){
            _this.addEventListener("touchstart", function(event) {
                // event.stopPropagation();
                timeout = setTimeout(function() {
                    var ele = $(event.target).closest(".live-msg-bubble").get(0);
                    if (ele && (ele.nodeName == "DL")) {
                        if (!popupMenuFlag) {
                            var ul = $("<ul/>", {
                                class: "dropdown-menu",
                                id: "popMenu"
                            });

                            var lis = "<li onclick=\"moveToLiveArea(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).find(".msg-content > p").text()+"\')\"><a href='#'>移到直播区</a></li>";
                            ul.append(lis);
                            $("body").append(ul);
                            setPostion(ul,event);
                            popupMenuFlag = true;
                        }
                    }
                }, 1000);
            }, false);
            _this.addEventListener("touchend", function() {
                clearTimeout(timeout);
            }, false);
            _this.addEventListener("touchstart", function(event) {
                if ($("#popMenu").length > 0) {
                    $("#popMenu").remove();
                    popupMenuFlag = false;
                }
            }, false);
            $(_this).attr("bindLongPress","yes")
        }
        
    });
}

function interaction_longPress_ForAdmin(managerList){

    var timeout = undefined;
    $("#mutual dl.msg-left").each(function() {
        var _this = this;
        if(undefined==$(_this).attr("bindLongPress")){
            _this.addEventListener("touchstart", function(event) {
                // event.stopPropagation();
                timeout = setTimeout(function() {
                    var ele = $(event.target).closest(".live-msg-bubble").get(0);
                    if (ele && (ele.nodeName == "DL")) {
                        if (!popupMenuFlag) {
                            var ul = $("<ul/>", {
                                class: "dropdown-menu",
                                id: "popMenu"
                            });

                            var lis = "<li onclick=\"disableTalk(\'"+$("#channelId_hidden").attr("channelid")+"\',\'"+$(_this).attr("userid")+"\')\"><a href='#'>禁言</a></li>"+
                                    "<li onclick=\"moveToLiveArea(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).find(".msg-content > p").text()+"\')\"><a href='#'>移到直播区</a></li>"+
                                    "<li onclick=\"goToReplyState(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).attr("msgid")+"\')\"><a href='#'>回复</a></li>";

                            if(managerList.indexOf($(_this).attr("userid")) < 0){
                                    lis += "<li onclick=\"grantManagerPermission(\'"+$(_this).attr("userid")+"\')\"><a href='#'>提拔ta为直播员</a></li>";
                            }
                            else{
                                    lis += "<li onclick=\"revokeManagerPermission(\'"+$(_this).attr("userid")+"\')\"><a href='#'>取消其直播员资格</a></li>";
                            }
                                    
                        
                            ul.append(lis);
                            $("body").append(ul);
                            setPostion(ul,event);
                            popupMenuFlag = true;
                        }
                    }
                }, 1000);
            }, false);
            _this.addEventListener("touchend", function() {
                clearTimeout(timeout);
            }, false);
            _this.addEventListener("touchstart", function(event) {
                if ($("#popMenu").length > 0) {
                    $("#popMenu").remove();
                    popupMenuFlag = false;
                }
            }, false);
            $(_this).attr("bindLongPress","yes")
        }
        
    });

     $("#mutual dl.msg-right").each(function() {
        var _this = this;
        if(undefined==$(_this).attr("bindLongPress")){
            _this.addEventListener("touchstart", function(event) {
                // event.stopPropagation();
                timeout = setTimeout(function() {
                    var ele = $(event.target).closest(".live-msg-bubble").get(0);
                    if (ele && (ele.nodeName == "DL")) {
                        if (!popupMenuFlag) {
                            var ul = $("<ul/>", {
                                class: "dropdown-menu",
                                id: "popMenu"
                            });

                            var lis = "<li onclick=\"moveToLiveArea(\'"+$(_this).find(".msg-author >span").text()+"\',\'"+$(_this).find(".msg-content > p").text()+"\')\"><a href='#'>移到直播区</a></li>";
                            ul.append(lis);
                            $("body").append(ul);
                            setPostion(ul,event);
                            popupMenuFlag = true;
                        }
                    }
                }, 1000);
            }, false);
            _this.addEventListener("touchend", function() {
                clearTimeout(timeout);
            }, false);
            _this.addEventListener("touchstart", function(event) {
                if ($("#popMenu").length > 0) {
                    $("#popMenu").remove();
                    popupMenuFlag = false;
                }
            }, false);
            $(_this).attr("bindLongPress","yes")
        }
        
    });
}


